﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Helpers;
using GdHotel.Data.Interfaces;
using GdHotel.Data.Resources;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageSideBlocksController : BaseController
    {
        private Size pictureSize = new Size(284, 144);

        public override ILanguagesRepository LanguagesRepository { get; set; }
        public ISideBlocksRepository SideBlocksRepository { get; set; }

        public ManageSideBlocksController(ISideBlocksRepository sideBlocksRepository, ILanguagesRepository languagesRepository)
        {
            SideBlocksRepository = sideBlocksRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /SideBlocks/

        public ViewResult Index()
        {
            var sideBlocks = SideBlocksRepository.FindAllByLang(Language);
            return View(sideBlocks);
        }

        //
        // GET: /SideBlocks/Details/5

        public ViewResult Details(Int32 id)
        {
            var sideBlock = SideBlocksRepository.GetById(id);
            return View(sideBlock);
        }

        //
        // GET: /SideBlocks/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SideBlocks/Create

        [HttpPost]
        public ActionResult Create(SideBlock sideBlock, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var picturePath = GraphicsHelper.SaveAndResizePicture(file, pictureSize, Server, Common.SideBlocksPicturesDirectory);
                sideBlock.PicturePath = picturePath ?? "";
                sideBlock.LanguageId = Language.Id;
                SideBlocksRepository.Insert(sideBlock);
                SideBlocksRepository.Save();
                return RedirectToAction("Index");
            }

            return View(sideBlock);
        }

        //
        // GET: /SideBlocks/Edit/5

        public ActionResult Edit(int id)
        {
            var sideBlock = SideBlocksRepository.GetById(id);
            return View(sideBlock);
        }

        //
        // POST: /SideBlocks/Edit/5

        [HttpPost]
        public ActionResult Edit(SideBlock sideBlock, HttpPostedFileBase file)
        {
            var dbSideBlock = SideBlocksRepository.GetById(sideBlock.Id);
            if (ModelState.IsValid)
            {
                var picturePath = dbSideBlock.PicturePath;
                try
                {
                    if (file != null)
                    {
                        picturePath = GraphicsHelper.SaveAndResizePicture(file, pictureSize, Server, Common.SideBlocksPicturesDirectory);
                    }
                }
                catch
                {
                }

                dbSideBlock.PicturePath = picturePath ?? "";
                UpdateModel(dbSideBlock);
                SideBlocksRepository.Save();
                return RedirectToAction("Index");
            }
            return View(sideBlock);
        }

        //
        // GET: /SideBlocks/Delete/5

        public ActionResult Delete(int id)
        {
            var sideBlock = SideBlocksRepository.GetById(id);
            return View(sideBlock);
        }

        //
        // POST: /SideBlocks/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var sideBlock = SideBlocksRepository.GetById(id);
            SideBlocksRepository.Delete(sideBlock);
            SideBlocksRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            SideBlocksRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}